
package Server;

import DAO.MySQLAccountDAO;
import DAO.MySQLAirportDAO;
import DAO.MySQLFlightDAO;
import Types.AccountType;
import java.util.ArrayList;
import java.util.Observable;

/**
 * The classs that receives a request and passes it onto the appropriate DAO and then sorts the result before sending back a reply
 * @author Samuel Millar
 */
public class DatabaseInterface extends Observable implements Runnable{
    private boolean updated = false;
    private ArrayList<String> reply = new ArrayList<String>();
    private AccountType account;
    private MySQLAccountDAO accountDao = new MySQLAccountDAO();
    private MySQLAirportDAO airportDao = new MySQLAirportDAO();
    private MySQLFlightDAO flightDao = new MySQLFlightDAO();
    private String username;
    private String password;
/**
     * Receives a request and sends it off to the appropriate DAO and then returns the result
     * @param request The Request from the client
 * @return reply The Reply from the DAO
     */
    public ArrayList<String> receiveRequest(ArrayList<String> request){
        System.out.println(request.get(0));
        if(request.get(0).equals("log")){
            username = request.get(1);
            password = request.get(2);
            account = accountDao.validAccount(username, password);
            if (account != null) {
                reply.add(0,"victory");
                reply.add(1, account.getAddress());
                reply.add(2, account.getFirstName());
                reply.add(3, account.getLastName());
                reply.add(4, account.getUsername());
                reply.add(5, account.getPassword());
                reply.add(6, account.getLevel());
            } else {
                reply.add(0,"defeat");
            }
        }
        if(request.get(0).equals("airportCreate")){
            airportDao.makeAirport(request.get(1), request.get(2), request.get(3));
            reply.add(0,"victory");
        }
        if(request.get(0).equals("airportDelete")){
            airportDao.deleteAirport(request.get(1));
            reply.add(0,"victory");
        }
        if(request.get(0).equals("airportEdit")){
            airportDao.editAirport(request.get(1), request.get(2), request.get(3));
            reply.add(0,"victory");
        }
        if(request.get(0).equals("flightCreate")){
            flightDao.addFlight(request.get(1), request.get(2), request.get(3), request.get(4), request.get(5), request.get(6), Integer.parseInt(request.get(7)), Float.parseFloat(request.get(8)));
            reply.add(0,"victory");
        }
        if(request.get(0).equals("flightDelete")){
            flightDao.deleteFlight(request.get(1), request.get(2), request.get(3), request.get(4), request.get(5), request.get(6), Integer.parseInt(request.get(7)), Float.parseFloat(request.get(8)));
            reply.add(0,"victory");
        }
        if(request.get(0).equals("flightEdit")){
            flightDao.editFlight(request.get(1), request.get(2), request.get(3), request.get(4), request.get(5), request.get(6), Integer.parseInt(request.get(7)), Float.parseFloat(request.get(8)));
            reply.add(0,"victory");
        }
        if(request.get(0).equals("getAirports")){
            reply = airportDao.getAirports();
            System.out.println(reply);
        }
        if(request.get(0).equals("getFlights")){
            reply = flightDao.getFlightsInfo();
            System.out.println(reply);
        }
        this.run();
        return reply;
    }
    
    /**
     * Notifyies observers of an update if there is one
     */
    public void run()
    {
        if (updated){
            final String RESPONSE = "response";
            setChanged();
            notifyObservers( RESPONSE );
        }
    }
}

